import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_date_type.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_file_upload_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_high_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_select_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/env/env.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/data/entities/home/duty/system_user_page_entity.dart';
import 'package:safetymonitor/router/app_pages.dart';

import 'controller.dart';
///处置中（蓝色和黄色）
class OtherDisposePage extends StatelessWidget {
  OtherDisposePage({super.key});

  final controller = Get.put(DisposeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        titleText: controller.entity.click!=11?'处置':'详情',
      ),
      body: GetBuilder(
        init: controller,
        builder: (_) {
          return SingleChildScrollView(
              child: controller.entity.click!=11//只有查看权限
                  ? Column(
                children: [
                  AppGaps.vGap10,
                  _getStateView(),
                  Obx(() {
                    return controller.currentIndex.value == 0
                        ? _getNormalView() // 基本信息
                        : _getExistenceView(); //
                  }),
                ],
              )
                  : _getNormalView() // 基本信息,
          );
        },
      ),
    );
  }
  Widget _getNormalView() {
    return Column(
        children: [
          _titleBg(),
          Container(
            height: 40.w,
            width: double.infinity,
            color: const Color(0xFFF4F7FF),
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(left: 40.w),
            child: Text(
              '信息',
              style: TextStyle(
                fontSize: 16.sp,
              ),
            ),
          ),
          Container(
            height: 1.w,
            color: const Color(0xFFDEE5FD),
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  AppGaps.vGap16,
                  CustomTitleChartPage(
                    contentPadding: EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
                    title: controller.entity.companyName,
                    titleRightWidget: controller.listController.getStatueView(controller.entity.status),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Expanded(child: _getTextView(title: '点位:', value: controller.entity.deviceCode)),
                            Expanded(child: _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}级别:', value: CustomDateType.getAlarmType(controller.entity.alarmLevel, controller.entity.dataType),),)
                          ],
                        ),
                        Row(
                          children: [
                            Expanded(child: _getTextView(title: '系统-类别:', value:   controller.entity.categoryZh,),),
                            Expanded(child: _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}数值:', value: '${controller.entity.val}')),
                          ],
                        ),
                        Row(
                          children: [
                            Expanded(child: _getTextView(title: '系统名称:', value: controller.entity.systemName)),
                            Expanded(child: _getTextView(title: '监测类型:', value: controller.entity.categoryZh.split('-').last)),
                          ],
                        ),
                        _getTextView(title: '报警类型:', value: CustomDateType.getAlarmTypeByValue(controller.entity.dataType)),
                        _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}开始:', value:controller.entity.alarmStartTime!=0? Jiffy.parseFromMillisecondsSinceEpoch(controller.entity.alarmStartTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                        _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}结束:', value: controller.entity.alarmEndTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(controller.entity.alarmEndTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                        // _getTextView(title: '报警结束:', value: DateTime.now().toStr),
                        AppGaps.vGap6,
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),

        ]
    );
  }
  /// 标题背景分隔栏
  Widget _titleBg() {
    return Column(
      children: [
        AppGaps.vGap12,
        Container(
          height: 2.w,
          color: const Color(0xFF587DF5),
        ),
      ],
    );
  }

  Widget _getExistenceView() {
    return Column(
      children: [
        _titleBg(),
        Container(
          color: Colors.white,
          padding:
          EdgeInsets.only(left: 20.w, right: 20.w, top: 20.w, bottom: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _chuZhi(),
              AppGaps.vGap20,
              Container(
                width: double.infinity,
                color: Colors.white,
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
                child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColors.blue_button,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(22.r),
                      ),
                    ),
                    onPressed: controller.confirm,
                    child: Text(
                      '确定',
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w400,
                      ),
                    )),
              )
            ],
          ),
        ),
      ],
    );
  }

  Widget _chuZhi() {
    return Column(children: [
      CustomInputPage(
        title:  '${CustomDateType.getDataType(controller.entity.dataType)}原因：',
        text:controller.reasonAlarm,
        hintText: '请输入报警原因',
        onCallBack: (value) {
          controller.reasonAlarm = value;
        },
      ),
      CustomHighInputPage(
          title: '${CustomDateType.getDataType(controller.entity.dataType)}描述：',
          text: controller.alarmDescription,
          hintText: '请输入报警描述',
          onCallBack: (value) {
            controller.alarmDescription = value;
          }),
      CustomSelectPage(
        selectText:  controller.onSiteHandlerName,
        title: '现场处理人：',
        onCallBack: () {
          Get.toNamed(Routes.systemUserPeopleSelectionSelectionPage,
              arguments: {
                'isRadio': true,
              })?.then((value) {
            final List<SystemUserPageEntity> selectedList =
            value as List<SystemUserPageEntity>;
            controller.onSiteHandlerId = selectedList[0].id; //获取人员id
            controller.onSiteHandlerName = selectedList[0].nickname!; //获取人员名字
            controller.departmentBelongsId = selectedList[0].deptId!; //部门id
            controller.departmentBelongsIdName = selectedList[0].deptName!; //部门
            controller.update();
          });
        },
      ),
      CustomSelectPage(
        isEnabled: false,
        selectText: controller.departmentBelongsIdName,
        title: '所属部门：',
        onCallBack: () {
        },
      ),
      CustomSelectPage(
          selectText:controller.onSiteProcessingTime,
          title: '现场处理时间：',
          onCallBack: () {
            controller.showDatePicker();
          }),
      CustomSelectPage(
        isEnabled: false,
        selectText:  UserManager.getUser()!.nickname,
        title: '负责人：',
        onCallBack: () {
        },
      ),
      CustomHighInputPage(
          title: '处置措施：',
          text: controller.disposalMeasures,
          hintText: '请输入处置措施',
          onCallBack: (value) {
            controller.disposalMeasures = value;
          }),
      CustomFileUploadPage(
        isShowDivider: false,
        splitCommaInUrl: true,
        isRequired: DartDefines.appInstallChannel == AppInstallChannel.kaifa ?false:true,
        // uploadedFiles: [],
        title: '处置附件(仅限上传格式：jpeg、png)',
        onCallBack: (value) {
          final String imgUrl = value.join(',');
          controller.disposalAttachments = imgUrl;
        },
      ),
    ],);
  }
  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
                  value,
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xff1D3452),
                  ),
                ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }

  Widget _getStateView() {
    return Obx(() {
      return Container(
        padding: EdgeInsets.symmetric(horizontal: 45.w),
        height: 64.w,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Positioned(
              left: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    controller.currentIndex.value == 0
                        ? 'assets/svg/icon_normal.svg'
                        : 'assets/svg/icon_un_normal.svg',
                    width: 143.w,
                    height: 40.w,
                  ),
                  Text(
                    '基本',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: controller.currentIndex.value == 0
                          ? const Color(0xFFffffff)
                          : const Color(0xFF9FA5B0),
                    ),
                  )
                ],
              ).clickOnce(() {
                controller.selectTitle(0);
              }),
            ),
            Positioned(
              right: 0,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    controller.currentIndex.value == 1
                        ? 'assets/svg/icon_existence.svg'
                        : 'assets/svg/icon_un_existence.svg',
                    width: 143.w,
                    height: 40.w,
                  ),
                  Text(
                    '处置信息',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: controller.currentIndex.value == 1
                          ? const Color(0xFFffffff)
                          : const Color(0xFF9FA5B0),
                    ),
                  )
                ],
              ).clickOnce(() {
                controller.selectTitle(1);
              }),
            ),
          ],
        ),
      );
    });
  }


}
